package com.tansun.easycare.rule.rulegroup.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.rulegroup.dao.RuleGroupRefDao;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroup;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroupRef;
import com.tansun.easycare.rule.rulegroup.service.RuleGroupRefService;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;

/**
 * 规则集规则关联关系ServiceImpl
 * @author lph
 * @version 2019-11-19
 */
@Service("ruleGroupRefService")
@Transactional
public class RuleGroupRefServiceImpl extends RuleBaseServiceImpl<RuleGroupRefDao, RuleGroupRef> implements RuleGroupRefService{

	public RuleGroupRef get(String id) {
		return super.get(id);
	}
	
	public List<RuleGroupRef> findList(RuleGroupRef ruleGroupRef) {
		return super.findList(ruleGroupRef);
	}
	
	public PageInfo<RuleGroupRef> findPage(Page<RuleGroupRef> page, RuleGroupRef ruleGroupRef) {
		return super.findPage(page, ruleGroupRef);
	}
	
	public void save(RuleGroupRef ruleGroupRef) {
		super.save(ruleGroupRef);
	}
	
	public void delete(RuleGroupRef ruleGroupRef) {
		super.delete(ruleGroupRef);
	}
	
	@Override
	public List<RuleGroup> findRuleGroupList(RuleGroup condition){
		return this.dao.findRuleGroupList(condition);
	}
	
	@Override
	public List<RuleGroup> findRefRuleManage(RuleGroup condition){
		return this.dao.findRefRuleManage(condition);
	}
    @Override
    public void shiftSort(RuleManage ruleManage, String id1, String id2, String shiftType) throws BizException {
    	
    	RuleGroupRef condition = new RuleGroupRef();
    	condition.setManageId(ruleManage.getId());
    	condition.setRuleId(id1);
    	condition.setVersion(ruleManage.getVersion());
    	List<RuleGroupRef> list = this.findList(condition);
    	if(list==null||list.size()==0) {
    		return;
    	}
        RuleGroupRef ruleVo1 = list.get(0);
        
        condition.setRuleId(id2);
        list = this.findList(condition);
    	if(list==null||list.size()==0) {
    		return;
    	}
        RuleGroupRef ruleVo2 = list.get(0);
        Integer sal = ruleVo1 == null ? 0 : ruleVo1.getSort();
        ruleVo1.setSort(ruleVo2.getSort());
        ruleVo2.setSort(sal);
        //直接切换到最末不再交换，而是当前最小优先级减一
/*        if ("bot".equals(shiftType)) {
            sal = ruleManage.getCountSalience() - 1;
            ruleManage.setCountSalience(sal);
            ruleVo1.setSort(sal);
        } else if ("top".equals(shiftType)) {
            ruleVo1.setSort(ruleVo2.getSort() + 1);
//				ruleVo2.setSort(sal);				
        } else {
            ruleVo1.setSort(ruleVo2.getSort());
            ruleVo2.setSort(sal);
        }*/
        this.save(ruleVo1);
        this.save(ruleVo2);
    }

	@Override
	public List<String> findManageIdByRuleId(String ruleId) {
		return this.dao.findManageIdByRuleId(ruleId);
	}

	@Override
	public void saveOrUpdate(RuleGroupRef ruleGroupRef) throws BizException {
		if(StringUtils.isNotEmpty(ruleGroupRef.getId())) {
			this.dao.update(ruleGroupRef);
		}else {
			this.save(ruleGroupRef);
		}
	}

	@Override
	public void deleteByManageID(RuleGroupRef ruleGroupRef) throws BizException {
		this.dao.deleteByManageID(ruleGroupRef);
	}

	@Override
	public List<String> findManageIdByRuleIds(List<String> ids) {
		if(ids!=null&&ids.size()>0) {
			return this.dao.findManageIdByRuleIds(ids);
		}else {
			return new ArrayList<String>();
		}
	}
	
	@Override
	public void deleteByMutilVersion(RuleManage ruleManage, List<String> versions){
		if(versions!=null&&versions.size()>0) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("projectCode", ruleManage.getProjectCode());
			map.put("manageId", ruleManage.getId());
			map.put("versions", versions);
			this.dao.deleteByMutilVersion(map);
		}
	}
}